package br.com.pnfp.web.control.actions.cadastros;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import br.com.pnfp.web.control.actions.DefaultAction;
import br.com.pnfp.web.control.forms.cadastros.CadastroCupomForm;
import br.com.pnfp.web.model.vo.Cupom;
import br.com.pnfp.web.model.vo.Doador;
import br.com.pnfp.web.model.vo.Usuario;

public class CadastroCupomAction extends DefaultAction {
	
	private static final Logger logger = Logger.getLogger(CadastroCupomAction.class); 
	
	public ActionForward iniciar(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		request.setAttribute("dataDe", getDateFormatDojo().format(new Date()));
		request.setAttribute("dataAte", getDateFormatDojo().format(new Date()));
		
		return mapping.findForward("body");
	}
	
	public ActionForward consultar(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {		
		
		CadastroCupomForm cupomForm = (CadastroCupomForm) form;
		consultar(cupomForm, request);
		
		return mapping.findForward("body");
	}	
	
	public ActionForward alterarIniciar(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {		
		
		CadastroCupomForm cupomForm = (CadastroCupomForm) form;
		List<Cupom> lista = getCupomDAO().consultar(new Cupom(cupomForm.getIdSelecionado(), getUsuario(request)));
		 
		Cupom vo = lista.get(0);
		
		String[] params = {
			vo.getDoador().getCnpj(),
			vo.getCoo(),
			getDateFormatDojo().format(vo.getData()),
			getDecimalFormat().format(vo.getValor()),
			vo.getCodigo().toString()
		};
		
		printJsonObjectToStream(params, response);
		
		return null;
	}	
	
	public ActionForward alterar(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {		
		
		CadastroCupomForm cupomForm = (CadastroCupomForm) form;
		Cupom vo = new Cupom();
		vo.setCodigo(cupomForm.getIdSelecionado());
		vo.setDoador(new Doador(cupomForm.getDoador()));
		vo.setData(getDateFormat().parse(cupomForm.getData()));
		vo.setValor(DecimalFormat.getInstance().parse(cupomForm.getValor()).doubleValue());
		vo.setCoo(cupomForm.getCoo());
		vo.setUsuarioSistema(getUsuario(request));
		
		getCupomDAO().alterar(vo);
		
		return null;
	}
	
	public ActionForward excluir(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {		
		
		CadastroCupomForm cupomForm = (CadastroCupomForm) form;
		try {
			getCupomDAO().excluir(cupomForm.getIdSelecionado());
			printJsonObjectToStream(new Object[] {true}, response);	
		} catch(Exception exc) {
			Object[] params = {false, "Nao e possivel excluir este Cupom!"};
			printJsonObjectToStream(params, response);
		}
		
		return null;
	}

	
	private void consultar(CadastroCupomForm cupomForm, HttpServletRequest request) throws Exception {
		Cupom vo = new Cupom();
		vo.setDataDe(getDateFormatDojo().parse(cupomForm.getDateDeConsultar()));
		vo.setDataAte(getDateFormatDojo().parse(cupomForm.getDateAteConsultar()));
		
		if(cupomForm.getUsuarioConsultar() != null && cupomForm.getUsuarioConsultar() > 0) {
			Usuario consulta = new Usuario();
			consulta.setCodigo(cupomForm.getUsuarioConsultar());
			consulta.setUsuarioSistema(getUsuario(request));
			
			Usuario usuario = getUsuarioDAO().consultar(consulta).get(0);
			vo.setUsuarioSistema(usuario);
		} else {
			vo.setUsuarioSistema(getUsuario(request));
		}
		
		vo.setUsuarioSistema(getUsuario(request));
		List<Cupom> listaResultado = getCupomDAO().consultar(vo);
		
		request.setAttribute("dataDe", getDateFormatDojo().format(vo.getDataDe()));
		request.setAttribute("dataAte", getDateFormatDojo().format(vo.getDataAte()));
		request.setAttribute("listaResultado", listaResultado);
	}
}
